clear;clc;

%% Setup
% Information Structure
Info            =   struct('ParamList',{{'b_lb','B_Total_SS'}});
Info.ParamInput =   struct('KAPPA',0.00/4,'XI',1/82.5/4,'Pir_SS',0.02/4,...
                            'TrProb_ext',0.92,'Prob_ext',0.33,...
                            'TrProb_H',0.98,'Prob_H',0.37,...
                            'Fin_DomShare',0.79,'Fin_AdjCost',1,...
                            'Cons_FracT',0.5,'Cons_FracH',0.6);
Info.Dim        =   length(Info.ParamList);

Info.StatName   =   {'Wealth-Income Ratio: Median',...'Median Wealth/Median Income','Prob. of Negative Wealth',...
                     'MPC: Median' ...,'Prob. of HtM','MPC: Average' ...
                     };
Info.Stat_Data  =   [0.35;0.15];

% Optimization 
ParaVec_0       =   [-1.0;0.4];               
lb              =   [-0.4;-0.2];
ub              =   [0.8 ;1.1];

ObjFunction     =   @(Input)Study_2_Calibration_Eval(Info,Input);

%% Global Optimization
warning('off','all');

%--------------------------------------------------------------------------
% Particle Swarm
%--------------------------------------------------------------------------
rng default
options             =   optimoptions('particleswarm',...
                                     'Display','iter',...'SwarmSize',100.'HybridFcn',@fmincon,..
                                     'MaxTime',60*60*1,'UseParallel',true);
[ParamVec,fval,exitflag] ...
                    =   particleswarm(ObjFunction,length(lb),lb,ub,options);

[~,Result]          =   ObjFunction(ParamVec);

save('Result_2_Calibration.mat','Info','Result','ParamVec');